﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace transmission
{
    /// <summary>
    /// TCPServerCreate.xaml 的交互逻辑
    /// </summary>
    public partial class TCPServerCreate : Window
    {
      
        public TCPServerCreate()
        {
            InitializeComponent();
            this.ResizeMode = ResizeMode.NoResize;
            Left = MainWindow.Instance.Left + MainWindow.Instance.Width / 2 - Width / 2;
            Top = MainWindow.Instance.Top + MainWindow.Instance.Height / 2 - Height / 2;
            portText.Text = string.Format("{0}", 5555);
            ipText.Text = string.Format("{0}", "0.0.0.0");
        }

        private void OnIPInputText(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !Regex.IsMatch(e.Text, @"^[1-9.]*\d*|0$");
        }

        private void OnPortInputText(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !Regex.IsMatch(e.Text, @"^[1-9]\d*|0$");
        }

        private void OnSureClicked(object sender, RoutedEventArgs e)
        {
            if(!Tools.ValidateIPAddress(ipText.Text))
            {
                MessageBox.Show("IP地址格式错误", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            if (!Tools.ValidatePort(portText.Text))
            {
                MessageBox.Show("Port格式错误", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            DialogResult = true;
        }

        public int GetPort()
        {
            return int.Parse(portText.Text.Trim());
        }

        public string GetIP()
        {
            return ipText.Text.Trim();
        }

        public bool GetIsPacket()
        {
            return isPacket.IsChecked == true;
        }

    }
}
